import namespace from "../namespace";
import extend from "lodash/extend";

class Oval extends namespace.VectorFigure {
  NAME = `${namespace.namespace}.shape.Oval`;

  constructor(attr, setter, getter) {
    super();

    this.setterWhitelist = extend(this.setterWhitelist, setter);

    this.getterWhitelist = extend(this.getterWhitelist, getter);

    super.attr(extend({}, attr));
  }

  createShapeElement() {
    let halfW = this.getWidth() / 2;
    let halfH = this.getHeight() / 2;

    return this.canvas.paper.ellipse(
      this.getAbsoluteX() + halfW,
      this.getAbsoluteY() + halfH,
      halfW,
      halfH
    );
  }

  getCenter() {
    let w2 = this.getWidth() / 2;
    let h2 = this.getHeight() / 2;

    return this.getPosition().translate(w2, h2);
  }

  setCenter(x, y) {
    let pos = new namespace.geo.Point(x, y);
    let w2 = this.getWidth() / 2;
    let h2 = this.getHeight() / 2;

    pos.translate(-w2, -h2);
    this.setPosition(pos);

    return this;
  }

  repaint(attributes) {
    if (this.repaintBlocked === true || this.shape === null) {
      return;
    }

    attributes = attributes || {};

    // don't override cx/cy if inherited class has set the center already.
    if (typeof attributes.rx === "undefined") {
      attributes.rx = this.width / 2;
      attributes.ry = this.height / 2;
    }

    // don't override cx/cy if inherited class has set the center already.
    if (typeof attributes.cx === "undefined") {
      attributes.cx = this.getAbsoluteX() + attributes.rx;
      attributes.cy = this.getAbsoluteY() + attributes.ry;
    }

    super.repaint(attributes);
  }
}

namespace.shape.Oval = Oval;
